/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.misc;

import org.bukkit.Location;

/**
 *
 * @author Dale
 */
public class DirectionSensitiveLocation extends Location {

    /**
     * Creates a direction sensitive location from a bukkit location
     * @param l Bukkit location to use in creating the location
     */
    public DirectionSensitiveLocation(Location l) {
        super(l.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
    }
    
    /**
     * Gets a location relative to the position of the current location
     * @param x Number of blocks right
     * @param y Number of blocks up
     * @param z Number of blocks forward
     * @return Relative block based on stored location and parameters
     */
    public DirectionSensitiveLocation getRelative(int x, int y, int z) {
        int truex = x;
        int truey = y;
        int truez = z;
        int old;
        
        // Rotate for pitch
        // 360 + pitch (Ensure positive)
        // value % 360 (ensure below 360)
        // value / 90.0 (obtain rotation as 0-4)
        // Math.round(value) (round to nearest int)
        switch((int) Math.round((((this.getPitch() + 360)%360)/90.0))) {
            case 1:
                // Rotate 90
                old = truey;
                truey = (0-truez);
                truez = old;
                break;
            case 2:
                // Rotate 180
                truey = (0-truey);
                truez = (0-truez);
                break;
            case 3:
                // Rotate 270
                old = truey;
                truey = truez;
                truez = (0-old);
                break;
        }
        
        // Rotate for yaw
        // yaw + 360 (Ensure positive)
        // value % 360 (ensure below 360)
        // value / 90.0 (obtain rotation as 0-4)
        // Math.round(value) (round to nearest int)
        switch((int) Math.round((((this.getYaw() + 360)%360)/90.0))) {
            case 3:
                // Rotate 270
                old = truex;
                truex = truez;
                truez = (0-old);
                break;
            case 2:
                // Rotate 180
                truex = (0-truex);
                truez = (0-truez);
                break;
            case 1:
                // Rotate 90
                old = truex;
                truex = (0-truez);
                truez = old;
                break;
        }
        
        // Create new location
        Location l = this.getBlock().getRelative(truex, truey, truez).getLocation();
        l.setYaw(this.getYaw());
        l.setPitch(this.getPitch());
        return new DirectionSensitiveLocation(l);
    }

    @Override
    public String toString() {
        return "DirectionSensitiveLocation{" + this.getWorld() + ": " + 
                this.getX() +"," + this.getY() +"," + this.getZ() + 
                " Pitch: " + this.getPitch() + " Yaw: " + this.getYaw() + "}";
    }
    
}
